When the user chooses the Open command from your File menu, you'll want to present a dialog box that allows the user to select the file to be opened. In Windows, this is normally done with the function GetOpenFileName , part of the Common Dialog Box Library. This function displays the standard Windows Open File dialog box on the screen, handles all interactions with the mouse and keyboard until the dialog is dismissed, and then returns a data structure of type OPENFILENAME identifying the file the user has selected. One of the members of this structure, lpstrFile , points to a string buffer in which to return the pathname of the file the user has selected. Ordinarily, a Windows program would simply pass this string to the appropriate Windows function, such as CreateFile , to open the designated file.
As we'll see in the next section, however, the QuickTime function OpenMovieFile instead expects to receive an analogous data structure from the Macintosh Standard File dialog package, a file-system specification record ( Listing 6 ).
Listing 6 File-system specification record
struct FSSpec
{
short vRefNum; // Volume reference number
long parID; // Directory ID of parent directory
Str255 name; // File name
}; /* end FSSpec */
So before calling OpenMovieFile from a Windows program, you have to create a specification record to pass to it. The QTML function FSMakeFSSpec does the job:
OSErr
FSMakeFSSpec
(short vRefNum, // Volume reference number
long dirID, // ID of parent directory
ConstStr255Param fileName, // File name
FSSpec *spec) // Returns a specification record
On the Macintosh, files are normally identified by giving a directory ID and a local file name within the directory. In Windows code, you set the directory ID and volume reference number to 0 and supply a full pathname instead; FSMakeFSSpec will interpret this correctly and initialize the specification record accordingly. Listing 7 shows how to use this function to mediate between the Windows common dialog box and the QTML OpenMovieFile function.
Another point to keep in mind is that the Windows GetOpenFileName function returns the file's pathname as a C-style string (terminated by a null character), whereas FSMakeFSSpec , like all QTML routines, expects it in Pascal form (preceded by a 1-byte length count).
QTML provides a pair of utility functions, c2pstr and p2cstr , for converting strings from one format to the other in place. You don't want to pass a string constant; the buffer needs to be modifiable.
Listing 7 Opening a user-selected movie file
OPENFILENAME ofn; // Parameters to Common Dialog Box
char pathName[255]; // Buffer for pathname
BOOL confirmed; // Did user confirm file selection?
FSSpec fileSpec; // File-system specification record
short theFile; // Reference number of movie file
HWND hwnd; // Handle to movie window
CGrafPtr windowPort; // Window's graphics port
OSErr errCode; // Result code
·
·
memset (&ofn, 0, sizeof(OPENFILENAME); // Clear to zero
fileName[0] = `\0'; // No default file name
ofn.lStructSize = sizeof(OPENFILENAME); // Size of structure
ofn.hwndOwner = GetActiveWindow(); // Active window owns dialog
ofn.lpstrFile = LPSTR(pathName); // Point to pathname buffer
ofn.nMaxFile = 255; // Size of buffer
ofn.lpstrFilter = "QuickTime Movies (*.mov;*.avi) \0 *.mov;*.avi\0";
// Filter string
ofn.nFilterIndex = 1; // Index of default filter
ofn.lpstrInitialDir = NULL; // Use current directory
confirmed = GetOpenFileName (&ofn); // Let user select file
if ( confirmed ) // Did user confirm selection?
{
c2pstr (pathName); // Convert to Pascal string
FSMakeFSSpec (0, 0L, pathName, &fileSpec); // Make specification record
windowPort = GetNativeWindowPort( hwnd ); // Get window's graphics port
SetGWorld (windowPort, nil); // Make it the graphics world
errCode = OpenMovieFile (&fileSpec, &theFile, fsRdPerm); // Open the movie file
} /* end if ( confirmed ) */
| Previous | Chapter Contents | Chapter Top | Roadmap | Next |